package com.service;

import com.bean.User;
import com.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.ServletRequest;
import java.util.UUID;

import static com.tool.Tool.getUserId;
import static com.tool.Tool.md5Hex;

/**
 * Created by Yuicon on 2017/3/19.
 */
@Service
public class UserService {

    @Autowired
    private UserMapper userMapper;

    public User findOne(ServletRequest req) {
        User user = userMapper.findById(getUserId(req));
        user.setGravatars(md5Hex(user.getEmail()));
        return user;
    }

    public User findByPhone(String phone) {
        return userMapper.findByPhone(phone);
    }

    public int insert(User user) {
        user.setId(UUID.randomUUID().toString());
        return userMapper.insert(user);
    }

    public int update(User user) {
        return userMapper.update(user);
    }
}
